package example.neon;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import static util.Util.getRealFile;

public class JPanelCheckbox extends JPanel {
    static BufferedImage check;
    static BufferedImage unCheck;

    static {
        try {
            check = ImageIO.read(getRealFile("img/neon/check.png"));
            unCheck = ImageIO.read(getRealFile("img/neon/uncheck.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    String title;
    String tip;
    public boolean isCheck;

    public JPanelCheckbox(String title, String tip, boolean setIsCheck) {
        this.isCheck = setIsCheck;
        this.title = title;
        this.tip = tip;
        int padding = 20;
        this.setBorder(BorderFactory.createEmptyBorder(padding, padding, padding, padding));
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                isCheck = !isCheck;
                repaint();
            }
        });
        this.setBackground(Color.WHITE);
    }

    public void setIsCheck(boolean isCheck) {
        this.isCheck = isCheck;
        this.repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        BufferedImage img = isCheck ? check : unCheck;
        g.drawImage(img, 4, 4, null);
        g.setColor(Color.BLACK);
        g.setFont(Commom.FONT_CONTENT);
        g.drawString(title, 42, 27);
        g.setColor(Color.GRAY);
        g.setFont(Commom.FONT_TIP);
        g.drawString(tip, 42, 44);
    }
}
